<?php
  require_once("../includes/config.php"); 
//Edit Pop-up"
  if($_SERVER["REQUEST_METHOD"] == "POST")
  {
    $name = $_POST['name'];
    $client_type = $_POST['client_type'];
    $nationality= $_POST['nationality'];
    $identification_type= $_POST['identification_type'];
    $identification_number =$_POST['identification_number'];
    $publication_place = $_POST['publication_place'];
    $expiry_date= $_POST['expiry_date'];
    $mobile = $_POST['mobile'];
    $phone= $_POST['phone'];
    $email=$_POST['email'];
    $workplace= $_POST['workplace'];
    $adress=$_POST['adress'];
    $Notes = $_POST['Notes'];
	//Only managers and admin can add notes
	if ($_SESSION['r']>1)
	{
		$Notes="";
	}
    $card_copy = $_POST['card_copy'];
	
	///////////////////////////////
	
	//$image_name =$_FILES["file"]["name"];
	//if ($_FILES["file"]["error"] > 0) {
    //echo "Error: " . $_FILES["file"]["error"] . "<br />";
	//move_uploaded_file($_FILES["file"]["tmp_name"], "images/client_ids/" . $_FILES["file"]["name"]);
	
	/////////////////////////////////
   $results = query("SELECT * FROM clients where identification_number = ? and owner_id=?",$identification_number,$_SESSION['user_id']);
   if(!empty($results) && $_POST['no-submit-related'] == 0)
   {
      $_SESSION['error_message'] = "رقم الإثبات مكرر"; 
      redirect("customers.php");
   }
   else if(!empty($results) && $_POST['no-submit-related'] == 1)
   {
    echo -2; //indicating error
   }
   else
   {
    
    //otherwise
      $result = query("INSERT INTO clients (name, client_type, nationality, identification_type,identification_number 
                     ,publication_place,expiry_date,mobile,phone,email,workplace,adress,Notes ,owner_id,card_copy) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ) ", 
        $name,$client_type,$nationality,$identification_type,$identification_number,$publication_place,$expiry_date ,
      $mobile,$phone,$email,$workplace,$adress,$Notes,$_SESSION['user_id'],$card_copy);
      
      if($_POST['no-submit-related'] == 1)
      {
        $id = query("getlastid");
        $result = query("SELECT id,name FROM clients where id=?",$id);
        $client = $result[0];
        echo $client['id'].':'.$client['name'];
      }
      else
      {
        $_SESSION['success_message'] = "تم إضافة العميل بنجاح"; 
        redirect("customers.php");
      }
    }
    

 
}
 
	?>